
#include "../Logs/wmglog.h"


// void test_log1()
// {
//     wmhLog::Logger::ptr logger=wmhLog::LoggerManager::getInstance().GetLogger("Async_logger");
//     logger->debug(__FILE__, __LINE__, "%s", "测试日志");
//     logger->info(__FILE__, __LINE__, "%s", "测试日志");
//     logger->warn(__FILE__, __LINE__, "%s", "测试日志");
//     logger->error(__FILE__, __LINE__, "%s", "测试日志");
//     logger->fatal(__FILE__, __LINE__, "%s", "测试日志");
    
//     size_t  count = 0;

//     std::string str = "测试日志-";

//     while (count<500000)
//     {
//         logger->fatal(__FILE__, __LINE__,"测试日志-%d",count++);
//     }
// }
void test_log2(const std::string& name)
{
    INFO("%s","开始测试");
    wmhLog::Logger::ptr logger=wmhLog::LoggerManager::getInstance().GetLogger(name);
    logger->debug("%s", "测试日志");
    logger->info("%s", "测试日志");
    logger->warn("%s", "测试日志");
    logger->error("%s", "测试日志");
    logger->fatal("%s", "测试日志");
    INFO("%s","结束测试");
    

    /**默认日志器测试*/
    // DEBUG("%s", "测试日志");
    // INFO("%s", "测试日志");
    // WARN("%s", "测试日志");
    // ERROR("%s", "测试日志");
    // FATAL("%s", "测试日志");
    
    // size_t  count = 0;

    // std::string str = "测试日志-";

    // while (count<500000)
    // {
    //     FATAL("测试日志-%d",count++);
    // }
}
int main()
{
    std::unique_ptr<wmhLog::LoggerBuilder> builder(new wmhLog::GlobalLoggerBuilder());
    builder->buildLoggerName("Async_logger");
    builder->buildLoggerLevel(wmhLog::logLevel::value::DEBUG);
    builder->buildFormatter("[%c][%f:%l][%p]%m%n");
    builder->buildLoggerType(wmhLog::LoggerType::LOGGER_ASYNC);
    // builder->buildEnableUnSafeAsync();非安全模式不提倡
    builder->buildSink<wmhLog::FileSink>("./logfile/sync.log");
    builder->buildSink<wmhLog::StdoutSink>();
    builder->buildSink<wmhLog::RollBySizeSink>("./logfile/sync-roll-size",1024*1024);
    //builder->buildSink<wmhLog::RollByTimeSink>("./logfile/Async-roll-time",wmhLog::TimeGap::GAP_SECOND);//拓展落地方法
    wmhLog::Logger::ptr logger=builder->build();
    test_log2("Async_logger");

    // time_t now=wmhLog::util::Date::getTime();
    // while (now+5>wmhLog::util::Date::getTime())
    // {
    //     logger->fatal("%s","时间落地测试");
    //     usleep(1000);
    // }
    









    // std::cout<<wmhLog::util::Date::getTime()<<std::endl;
    // std::string pathname="./abc/bcd/a.txt";
    // wmhLog::util::File::createDirectory(wmhLog::util::File::path(pathname));

    // wmhLog::logLevel::toString(wmhLog::logLevel::value::DEBUG);
    // std::cout<< wmhLog::logLevel::toString(wmhLog::logLevel::value::DEBUG)<<std::endl;
    // std::cout<< wmhLog::logLevel::toString(wmhLog::logLevel::value::INFO)<<std::endl;
    // std::cout<< wmhLog::logLevel::toString(wmhLog::logLevel::value::WARN)<<std::endl;
    // std::cout<< wmhLog::logLevel::toString(wmhLog::logLevel::value::ERROR)<<std::endl;
    // std::cout<< wmhLog::logLevel::toString(wmhLog::logLevel::value::DEBUG)<<std::endl;
    // std::cout<< wmhLog::logLevel::toString(wmhLog::logLevel::value::OFF)<<std::endl;

    // wmhLog::message msg(wmhLog::logLevel::value::DEBUG, 66, "man.cc", "root", "简单测试");
    // // wmhLog::Formatter fmt("abc%%ab[%d{%H:%M:%S}]%m%n");
    // // std::cout<<fmt.format(msg)<<std::endl;;
    // wmhLog::Formatter fmt;
    // std::string str = fmt.format(msg);
    // wmhLog::LogSink::ptr stdout_lsp=wmhLog::SinkFactor::create<wmhLog::StdoutSink>();
    // wmhLog::LogSink::ptr file_lsp=wmhLog::SinkFactor::create<wmhLog::FileSink>("./logfile/test.log");
    // wmhLog::LogSink::ptr roll_lsp=wmhLog::SinkFactor::create<wmhLog::RollBySizeSink>("./logfile/roll-",1024*1024);

    // stdout_lsp->log(str.c_str(),str.size());
    // file_lsp->log(str.c_str(),str.size());
    // size_t cursize=0;
    // size_t count=0;
    // while (cursize<1024*1024*10)
    // {
    //     std::string tmp=str+std::to_string(count++);
    //     roll_lsp->log(tmp.c_str(),tmp.size());
    //     cursize+=tmp.size();
    // }

    // wmhLog::LogSink::ptr time_lsp = wmhLog::SinkFactor::create<wmhLog::RollByTimeSink>("./logfile/roll-", wmhLog::TimeGap::GAP_MINUTE);

    // std::cout<<wmhLog::util::Date::getTime()<<std::endl;
    // time_lsp->log(str.c_str(),str.size());
    // sleep(10);;
    // std::cout<<wmhLog::util::Date::getTime()<<std::endl;
    // time_lsp->log(str.c_str(),str.size());
    // sleep(55);;
    // std::cout<<wmhLog::util::Date::getTime()<<std::endl;
    // time_lsp->log(str.c_str(),str.size());
    // sleep(62);;
    // std::cout<<wmhLog::util::Date::getTime()<<std::endl;
    // time_lsp->log(str.c_str(),str.size());

    // while (wmhLog::util::Date::getTime()<old+120)
    // {
    //     time_lsp->log(str.c_str(),str.size());
    //     //sleep(10);
    // }

    // std::string logger_name = "sync_logger";
    // wmhLog::logLevel ::value limit = wmhLog::logLevel::value ::WARN;

    // wmhLog::Formatter ::ptr fmt(new wmhLog::Formatter("[%d{%H:%M:%S}][%t][%c][%f:%l][%p]%T%m%n"));

    // wmhLog::LogSink::ptr stdout_1sp = wmhLog::SinkFactor::create<wmhLog::StdoutSink>();

    // wmhLog::LogSink::ptr file_1sp = wmhLog::SinkFactor::create<wmhLog::FileSink>("./logfile/test.log");

    // wmhLog::LogSink::ptr roll_lsp = wmhLog::SinkFactor::create<wmhLog::RollBySizeSink>("./logfile/roll-", 1024 * 1024);

    // std::vector<wmhLog::LogSink::ptr> sinks = {stdout_1sp, file_1sp, roll_lsp};

    // wmhLog::Logger::ptr logger(new wmhLog::SyncLogger(logger_name, limit, fmt, sinks));


    
    // while (cursize<1024*1024*10)
    // {
    //     std::string tmp=str+std::to_string(count++);
    //     roll_lsp->log(tmp.c_str(),tmp.size());
    //     cursize+=tmp.size();
    // }



    // std::ifstream ifs("./logfile/test.log",std::ios::binary);
    // if(ifs.is_open()==false) return -1;
    // ifs.seekg(0,std::ios::end);//将读写位置跳转到文件末尾
    // size_t fsize=ifs.tellg();//获取当前位置相对于起始位置的偏移量
    // ifs.seekg(0,std::ios::beg);//跳转到起始位置
    // std::string body;
    // body.resize(fsize);
    // ifs.read(&body[0],fsize);
    // if(ifs.good()==false) {std::cout<<"read error"<<std::endl;return -1;}
    // ifs.close();

    // wmhLog::Buffer buffer;
    // for(int i=0;i<body.size();i++)
    // {
    //     buffer.push(&body[i],1);
    // }

    // int ablesize=buffer.readerAbleSize();
    // std::ofstream ofs("./logfile/tmp.log",std::ios::binary);

    // for(int i=0;i<ablesize;++i)
    // {
    //     ofs.write(buffer.begin(),1);
    //     if(ofs.good()==false) {std::cout<<"read error"<<std::endl;return -1;}
    //     buffer.movereader(1);
    // }

    // ofs.close();
    return 0;
}
